﻿using TianYu.Core.Common;
using System.Text.RegularExpressions;

namespace TianYu.Core.Pdf
{
    public class ElementTools
    {
        public static PdfDocumentContent CreateElement(string templateStr)
        {
            //处理缓存时生成的符号  
            templateStr = Regex.Unescape(templateStr);

            var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(templateStr);
            var pdfDocumentContent = new PdfDocumentContent
            {
                Height = jsonObject.IntValue("Height"),
                Width = jsonObject.IntValue("Width"),
                MarginBottom = jsonObject.IntValue("MarginBottom"),
                MarginLeft = jsonObject.IntValue("MarginLeft"),
                MarginRight = jsonObject.IntValue("MarginRight"),
                MarginTop = jsonObject.IntValue("MarginTop"),
                Repeater = jsonObject.StringValue("Repeater")
            };
            var JsonElements = jsonObject["Elements"];
            if (JsonElements != null)
            {
                foreach (var item in JsonElements)
                {
                    var element = CreateElement(item, pdfDocumentContent.Height, pdfDocumentContent.MarginLeft, pdfDocumentContent.MarginBottom);
                    pdfDocumentContent.Elements.Add(element);
                }
            }
            if (pdfDocumentContent.Elements == null || pdfDocumentContent.Elements.Count == 0)
            {
                throw new TYBaseException("标签模板不能为空");
            }
            return pdfDocumentContent;
        }

        private static IPdfElement CreateElement(Newtonsoft.Json.Linq.JToken jsonToken, int height, float marginLeft, float marginBottom)
        {
            var jsonType = GetElementType(jsonToken.StringValue("Type"));
            var element = (IPdfElement)jsonToken.ToObject(jsonType);

            if (element != null)
            {
                //加上白边
                element.X = element.X + (int)marginLeft;
                element.Y = element.Y + (int)marginBottom;

                //转换Y坐标 以适应itextsharp 坐标以左下角为起始点的特性
                element.Y = height - element.Y - element.Height;
            }

            return element;
        }

        private static IDictionary<String, Type> _elementMap;
        private static Type GetElementType(string jsonType)
        {
            Check.NotEmpty(jsonType, nameof(jsonType));
            // 初始化映射表
            if (_elementMap == null)
            {
                var elementMap = new Dictionary<String, Type>(StringComparer.OrdinalIgnoreCase);
                foreach (var item in typeof(IPdfElement).GetAllSubclasses())
                {
                    var key = item.Name.TrimEnd("Element");
                    elementMap[key] = item;
                }
                _elementMap = elementMap;
            }

            _elementMap.TryGetValue(jsonType, out var type);
            if (type is null)
            {
                throw new ArgumentNullException(nameof(jsonType), "Elements>Type无效");
            }
            return type;
        }
    }
}
